package MainTest;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import factory.IncidentFactory;
import factory.InformationFactory;
import factory.NotificationFactory;
import factory.PositionFactory;

import Database.DataBaseAccessor;
import Parser.Parser;
import beans.Incident;
import beans.Information;
import beans.Notification;
import beans.Position;

public class Creator {

	 public static Logger log = Logger.getLogger("log_file");
	 FileHandler hand;
	
	//private int typeSource;
	private int idSource;
	private int idInformation;
	private int idStation;
	private int idLigne;
	private int typeInformation;
	
	InformationFactory NotifFact;
	InformationFactory PosFact ;
	InformationFactory IncidFact;
	
	Information inf;
	private String req;
	ResultSet result;
	
	public Creator() {
		
		try {
			hand = new FileHandler("MonLog.txt");
			log.addHandler(hand);
			SimpleFormatter formatter=new SimpleFormatter();
			hand.setFormatter(formatter);
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		PosFact = new PositionFactory();
		NotifFact = new NotificationFactory();
		IncidFact = new IncidentFactory();
		
		inf=null;
	}
	
	public Object Create(){
		String req="select idstation, idligne, description, typeInformation " +
				"from Information, Source " +
				"where IdInformation="+idInformation+" " +
				"and idSource="+idSource;
		
		System.out.println(req);
		
		DataBaseAccessor dbaccess=new DataBaseAccessor();
		
		ResultSet result= dbaccess.select(req);
		
		try {
			result.next();
			typeInformation=result.getInt("typeInformation");
			System.out.println("type inf : "+typeInformation);
			idStation=result.getInt("idstation");
			idLigne=result.getInt("idligne");
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(typeInformation==1)
			return (Object)createPosition();
		if(typeInformation==2)
			return (Object)createNotification();
		if(typeInformation==3)
			return (Object)createIncident();
		return null;
	}
	
	public Notification createNotification(){
		try{
		 inf=NotifFact.getInformation();
		 inf.setIdTrain(idSource);
		 inf.setIdLigne(idLigne);
		 inf.setIdStation(idStation);
		 inf.setCodeMessage(idInformation);
		 
		 Notification notif=(Notification)inf;
		  log.info("Notification créée : ");
		  log.info(notif.toString());
		 System.out.println("Notification créée : ");
			System.out.println(notif.toString());
		try {
			notif.setDescription(result.getNString("description"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		
		return notif;
		}catch(Exception e){
			return null;
		}
	}
	
	public Position createPosition(){
		
		inf=PosFact.getInformation();
		
		
		 inf.setIdTrain(idSource);
		 inf.setIdLigne(idLigne);
		 inf.setIdStation(idStation);
		 inf.setCodeMessage(idInformation);
		 
		 Position pos=(Position)inf;
		 log.info("Position crée : ");
		 log.info(pos.toString());
		 System.out.println("Position créée : ");
		System.out.println(pos.toString());
		return pos;
	}

	public Incident createIncident(){
		inf=IncidFact.getInformation();
		 inf.setIdTrain(idSource);
		 inf.setIdLigne(idLigne);
		 inf.setIdStation(idStation);
		 inf.setCodeMessage(idInformation);
		 
		 Incident incid=(Incident)inf;
		
		DataBaseAccessor dbaccess=new DataBaseAccessor();
		ResultSet result= dbaccess.select(req);
		
		try {
			incid.setCodeIncident(result.getNString("description"));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return incid;

	}

	public int getIdSource() {
		return idSource;
	}

	public void setIdSource(int idSource) {
		this.idSource = idSource;
	}

	public int getIdInformation() {
		return idInformation;
	}

	public void setIdInformation(int idInformation) {
		this.idInformation = idInformation;
	}

	public int getIdStation() {
		return idStation;
	}

	public void setIdStation(int idStation) {
		this.idStation = idStation;
	}

	public int getIdLigne() {
		return idLigne;
	}

	public void setIdLigne(int idLigne) {
		this.idLigne = idLigne;
	}
	
	public int getTypeInformation() {
		return typeInformation;
	}

	public void setTypeInformation(int typeInformation) {
		this.typeInformation = typeInformation;
	}
	
	public boolean setTrame(byte[] trame){
		Parser parcer=new Parser();
		parcer.Marshaler(trame);
		idInformation= parcer.getCoreParser().getIdInformation();
		idSource=parcer.getCoreParser().getIdSource();
		 log.info("Creator => idSource="+idSource+"  idInformation="+idInformation);
		System.out.println("Creator => idSource="+idSource+"  idInformation="+idInformation);
		Create();
		return true;
	}
}
